package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.dao.entity.*;
import com.jdd.dao.mapper.*;
import com.jdd.dao.service.TAreaService;
import com.jdd.dao.vo.AreaHouseNum;
import com.jdd.dao.vo.AreaNum;
import com.jdd.dao.vo.AreaNumVo;
import com.jdd.dao.vo.PeopleNum;
import com.jdd.modules.people.model.TAreaTreeModel;
import com.jdd.modules.people.util.FindAreaChildrenUtil;
import com.jdd.modules.people.vo.AreaSearchVo;
import com.jdd.modules.people.vo.AreaVo;
import com.jdd.modules.userstaff.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-23
 */
@Slf4j
@Service
public class TAreaServiceImpl extends ServiceImpl<TAreaMapper, TArea> implements TAreaService {

    @Resource
    private TProjectMapper tProjectMapper;
    @Resource
    private TAreaMapper tAreaMapper;

    @Resource
    private TBuildingMapper tBuildingMapper;

    @Resource
    private TUnitMapper tUnitMapper;

    @Resource
    private TFloorMapper tFloorMapper;

    @Resource
    private THouseMapper tHouseMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserMapper tUserMapper;
    @Override
    public List<TAreaTreeModel> queryTreeList() {
        LambdaQueryWrapper<TArea> query = new LambdaQueryWrapper<TArea>();
        query.orderByAsc(TArea::getSort);
        List<TArea> list = this.list(query);
        // 调用wrapTreeDataToTreeList方法生成树状数据
        List<TAreaTreeModel> listResult = FindAreaChildrenUtil.wrapTreeDataToTreeList(list);
        return listResult;
    }

    @Override
    public List<TAreaTreeModel> searhBy(String keyWord) {
        LambdaQueryWrapper<TArea> query = new LambdaQueryWrapper<TArea>();
        query.like(TArea::getName, keyWord);
        //update-begin--Author:huangzhilin  Date:20140417 for：[bugfree号]组织机构搜索回显优化--------------------
        TAreaTreeModel model = new TAreaTreeModel();
        List<TArea> areaList = this.list(query);
        List<TAreaTreeModel> newList = new ArrayList<>();
        if(areaList.size() > 0) {
            for(TArea area : areaList) {
                model = new TAreaTreeModel(area);
                model.setChildren(null);
                //update-end--Author:huangzhilin  Date:20140417 for：[bugfree号]组织机构搜索功回显优化----------------------
                newList.add(model);
            }
            return newList;
        }
        return null;
    }

    @Override
    public IPage<AreaVo> getAreaInfoPage(Page<AreaVo> page, AreaSearchVo areaSearchVo,List<String> projectCodes) {

        IPage<AreaVo> areaVoIPage = tAreaMapper.selectAreaVoPage(page, areaSearchVo,projectCodes);
        List<AreaVo> areaVoList = areaVoIPage.getRecords();
        /*代码优化*/
        /*获取都areaId集合*/
        List<String> areaIdList = areaVoList.stream().map(AreaVo::getId).collect(Collectors.toList());
        List<AreaHouseNum> areaHouseNums =  tHouseMapper.selectByAreaIds(areaIdList);
        List<PeopleNum> peopleNums = userMapper.selectPeopleNum();
        Map<String,PeopleNum> peopleNumMap = peopleNums.stream().collect(Collectors.toMap(PeopleNum::getAreaId,Function.identity()));
        Map<String,AreaHouseNum> areaHouseNumMap = areaHouseNums.stream().collect(Collectors.toMap(AreaHouseNum::getAreaId, Function.identity()));
        List<AreaNum> areaNums = tAreaMapper.selectAreaNum(areaIdList);
        Map<String,AreaNum> areaNumMap = areaNums.stream().collect(Collectors.toMap(AreaNum::getAreaId, Function.identity()));
        for (AreaVo areaVo : areaVoList) {
            AreaHouseNum areaHouseNum = areaHouseNumMap.get(areaVo.getId());
            PeopleNum peopleNum = peopleNumMap.get(areaVo.getId());
            AreaNum areaNum = areaNumMap.get(areaVo.getId());
            areaVo.setHouseNum(areaHouseNum.getHouseNum());
            areaVo.setParentName(areaNum.getParentName());
            areaVo.setChildrenAreaNum(areaNum.getChildrenNum());
            areaVo.setPeopleNum(peopleNum.getPeopleNum());
        }
        return areaVoIPage;
    }


    /**
     * @Description:根据区域id找到所属项目
     *
     * @param: areaId
     * @return: void
     * @Author: ls
     * @Date 2022/9/23 15:04
     */
    private TProject findProject(String parentId) {
        TProject tProject =null;
        TArea tArea = tAreaMapper.selectById(parentId);
        if (tArea!=null){
            tProject = findProject(tArea.getParentId());
        }else {
            tProject = tProjectMapper.selectById(parentId);

        }
        return tProject;
    }

    /*传入项目id获取房屋数和人口总数*/
    private AreaNumVo getNum(String areaId) {
        int houseNum = 0;
        int peopleNum = 0;
        List<TBuilding> tBuildings = tBuildingMapper.selectByAreaId(areaId);
        AreaNumVo areaNumVo = new AreaNumVo(0,0);
        areaNumVo.setPeopleNum(peopleNum);
        areaNumVo.setHouseNum(houseNum);
        return areaNumVo;
    }

    /*获取项目下的所有房屋数和人口数*/
    public AreaNumVo getNumByProject(){

        return null;
    }
    /*获取区域下所有项目的房屋数目和人口数*/
    public AreaNumVo getHouseNum(String areaId){
        AreaNumVo areaNumVo = new AreaNumVo(0,0);
        int houseNum =0;
        int peopleNum = 0;
        List<TBuilding> tBuildings = tBuildingMapper.selectByAreaId(areaId);
        for (TBuilding tBuilding : tBuildings) {
            List<TUnit> tUnits = tUnitMapper.selectByBuildingId(tBuilding.getId());
            for (TUnit tUnit : tUnits) {
                List<TFloor> tFloors = tFloorMapper.selectByUnitId(tUnit.getId());
                for (TFloor tFloor : tFloors) {
                    List<THouse> tHouses = tHouseMapper.selectByFloorId(tFloor.getId());
                    houseNum +=tHouses.size();
                    for (THouse tHouse : tHouses) {
                        int num = tUserMapper.selectByHouseId(tHouse.getId());
                        peopleNum+=num;
                    }
                }
            }

        }
        areaNumVo.setHouseNum(houseNum);
        areaNumVo.setPeopleNum(peopleNum);
        return areaNumVo;
    }
}
